import 'package:flutter/material.dart';
import 'package:sweet_app/bloc/application_bloc.dart';
import 'package:sweet_app/bloc/bloc_provider.dart';
import 'package:sweet_app/bloc/search_bloc.dart';
import 'package:sweet_app/buttons/color_loader_5.dart';
import 'package:sweet_app/buttons/dot_type.dart';
import 'package:sweet_app/http/api.dart';
import 'package:sweet_app/model/user.dart';
import 'package:sweet_app/utils/utils.dart';

class AddFriendPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final searchBloc = BlocProvider.of<ApplicationBloc>(context);
    return Scaffold(
      appBar: AppBar(title: Text("添加朋友")),
      body: Padding(
        padding: EdgeInsets.all(10.0),
        child: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Builder(builder: (context) {
                  return Expanded(
                      child: TextField(
                    onSubmitted: (value) =>
                        _searchFriend(context, value, searchBloc),
                    keyboardType: TextInputType.phone,
                    decoration: InputDecoration(
                      hintText: "请输入手机号",
                      prefixIcon: Icon(Icons.search),
                      border: OutlineInputBorder(),
                    ),
                  ));
                }),
              ],
            ),
            const SizedBox(height: 5.0),
            StreamBuilder<SearchBody>(
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return SearchItem(
                        user: snapshot.data.user,
                        friendFlag: snapshot.data.friendFlag,
                        onTap: () async {
                          ResultModel model = await Api.addFriend(snapshot.data.user);
                          if(model.type == ResultType.success) {
                            showModel(context, "发送成功");
                          } else {
                            showModel(context, model.error);
                          }
                        });
                  }
                  return Container(width: 0.0, height: 0.0);
                },
                stream: searchBloc.searchStream)
          ],
        ),
      ),
    );
  }

  void _searchFriend(
      BuildContext context, String value, ApplicationBloc bloc) async {
    if (value.isNotEmpty) {
      print("phone: $value");
      final ResultModel model = await Api.searchFriend(phone: value);
      print('model: $model');
      if (model.type == ResultType.success) {
        bloc.add(SearchBody(
            user: User.fromJson(model.data['user']),
            friendFlag: model.data['friendFlag']));
      } else {
        showModel(context, model.error);
      }
    }
  }
}

class SearchItem extends StatelessWidget {
  final User user;
  final bool friendFlag;
  final VoidCallback onTap;

  SearchItem({this.user, this.friendFlag, this.onTap});

  @override
  Widget build(BuildContext context) {
    return user != null
        ? Container(
            height: 80.0,
            child: ListTile(
              leading: CircleAvatar(
                child: Text(user.name[0]),
              ),
              title: Text(user.name),
              subtitle: Text(user.phone),
              trailing: friendFlag
                  ? RaisedButton(onPressed: onTap, child: Text("查看"))
                  : RaisedButton(onPressed: onTap, child: Text("添加")),
            ),
          )
        : Container(width: 0.0, height: 0.0);
  }
}
